import {createStore} from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
	state: {
		token: '',
		axiosPromiseCancel: [] // 接口请求存放的数组
	},
	mutations: {
		/*设置token*/
		setToken (state, data) {
			state.token = data || ''
		},
		/*请求依次添加到数组中*/
		setAxiosPromiseCancelArr(state, cancel) {
			state.axiosPromiseCancel.push(cancel)
		},
		/*取消数组中的所有请求，并重置为空数组*/
		clearAxiosPromiseCancelArr(state) {
			if(state.axiosPromiseCancel.length!==0){
				state.axiosPromiseCancel.forEach(e=>{
					e && e.f()
				})
				state.axiosPromiseCancel = []
			}
		},
		/*取消当前数组中的重复请求*/
		clearCurrentAxiosPromiseCancel(state, index){
			state.axiosPromiseCancel[index].f()
			state.axiosPromiseCancel.splice(index, 1)
		}
	},
	actions: {},
	getters: {
		getToken (state) {
			return state.token
		}
	},
	modules: [],
	plugins: [
		createPersistedState({
			storage: window.sessionStorage,
			reducer (value, path) {
				return {
					token: value.token
				}
			}
		})
	]
})
